package org.example.client.demo01;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import lombok.extern.slf4j.Slf4j;
import org.example.client.demo01.handler.FirstClientHandler;

@Slf4j
public class NettyClientApp {

    public static void main(String[] args) {
        String HOST = "127.0.0.1";
        int PORT = 8080;
        EventLoopGroup group = new NioEventLoopGroup();
        try {
            Bootstrap b = new Bootstrap();
            b.group(group)
                    .channel(NioSocketChannel.class)
                    .handler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch) throws Exception {
                            ChannelPipeline p = ch.pipeline();
                            p.addLast(new FirstClientHandler());
                        }
                    });

            // 连接服务器
            //ChannelFuture f = b.connect(HOST, PORT).sync();//生产环境中不建议使用sync();
            ChannelFuture f = b.connect(HOST, PORT);
        }catch (Exception e){
            log.error("客户端失败",e);
        }
    }
}
